package com.inspur.sc.springboot.controller;

import com.inspur.sc.springboot.convert.SysUserDTOConvertSysUser;
import com.inspur.sc.springboot.dto.SysUserDTO;
import com.inspur.sc.springboot.entity.SysRoleUser;
import com.inspur.sc.springboot.entity.SysUser;
import com.inspur.sc.springboot.service.SysRoleUserService;
import com.inspur.sc.springboot.service.SysUserService;
import com.inspur.sc.springboot.utils.ResponseUtil;
import com.inspur.sc.springboot.utils.SpringSecurityUtils;
import com.inspur.sc.springboot.vo.DataTablesVO;
import com.inspur.sc.springboot.vo.QueryVO;
import com.inspur.sc.springboot.vo.ResponseVO;
import com.inspur.sc.springboot.vo.SysUserVO;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleUserService roleUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("findByUsernameLike")
    public DataTablesVO<SysUser> findByUsernameLike(QueryVO queryVO){

        Integer pageNo=queryVO.getStart()/queryVO.getLength();
        Pageable pageable=PageRequest.of(pageNo,queryVO.getLength());
        Page<SysUser> page= userService.findByUsernameLike("%"+queryVO.getSearch()+"%",pageable);
        DataTablesVO<SysUser> result=new DataTablesVO<>(page.getContent(),page.getTotalElements(),page.getTotalElements(),queryVO.getDraw());
        return result;
    }

    @PostMapping("save")
    public ResponseEntity<ResponseVO> save(@RequestBody SysUserDTO userDTO){
        SysUser user=SysUserDTOConvertSysUser.convert(userDTO);
        boolean isSave=userService.saveUser(user,userDTO.getRoleIds());
        if(isSave){
            return ResponseUtil.success("保存成功",true);
        }
        return ResponseUtil.success("保存失败",false);
    }

    @PostMapping("getUser")
    public ResponseEntity<ResponseVO> getUser(@RequestParam(value = "id") Integer id){
        SysUserVO userVO=userService.findUserById(id);
        if(null != userVO){
            return ResponseUtil.success(userVO);
        }
        return ResponseUtil.success(null);
    }

    @PostMapping("delete")
    public ResponseEntity<ResponseVO> delete(@RequestParam(value = "id") Integer id){
        boolean isDel=userService.deleteUserById(id);
        if(isDel){
            return ResponseUtil.success("删除成功!");
        }
        return ResponseUtil.success("删除失败!");
    }

    @PostMapping("updatePwd")
    public ResponseEntity<ResponseVO> updatePwd(HttpServletRequest request,
                                                @RequestParam("oldPassword") String oldPassword,
                                                @RequestParam("newPassword") String newPassword){
        Authentication authentication=SpringSecurityUtils.getAuthentication(request);
        SysUser sysUser= (SysUser) authentication.getPrincipal();
        if(passwordEncoder.matches(oldPassword,sysUser.getPassword())){
            sysUser.setPassword(passwordEncoder.encode(newPassword));
            boolean isSave=userService.saveUser(sysUser,null);
            if(isSave){
                return ResponseUtil.success("密码已修改，请重新登录!",true);
            }else {
                return ResponseUtil.success("密码修改失败!",false);
            }
        }else{
            return ResponseUtil.success("原密码不正确，请重新输入！",true);
        }
    }

    /**
     * 管理员对用户密码进行重置，重置密码为123456
     * @param id
     * @return
     */
    @PostMapping("restPwd")
    public ResponseEntity<ResponseVO> restPwd(@RequestParam("id") Integer id){
        boolean is=userService.restPwd(id);
        if(is){
            return ResponseUtil.success("密码重置成功!",true);
        }
        return ResponseUtil.success("密码重置失败!",false);
    }

    @GetMapping("getUserSkin")
    public ResponseEntity<ResponseVO> getUserSkin(HttpServletRequest request){
        Authentication authentication=SpringSecurityUtils.getAuthentication(request);
        SysUser sysUser= (SysUser) authentication.getPrincipal();
        return ResponseUtil.success(sysUser.getSkin());
    }

    @GetMapping("setUserSkin")
    public ResponseEntity<ResponseVO> setUserSkin(HttpServletRequest request,
                                                  @RequestParam("skin") String skin){
        Authentication authentication=SpringSecurityUtils.getAuthentication(request);
        SysUser sysUser= (SysUser) authentication.getPrincipal();
        sysUser.setSkin(skin);
        boolean isSave=userService.saveUser(sysUser,null);
        if(isSave){
            return ResponseUtil.success("主题设置成功!",true);
        }
        return ResponseUtil.success("主题设置失败!",false);
    }
}
